package org.testtools.handler;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.testtools.bean.Template;
import org.testtools.util.IdGenUtil;

public class TemplateHandler {

    private static final String BLANK = "";

    private static final String FIXED = "FIXED";

    private static final String DATE = "DATE";

    private static final String HEXA = "HEXA";

    private static final String INT = "INT";

    private static final String PATTERN = "PATTERN";

    private static final String COLON = ":";

    private static final String LINEBREAK = "\n";

    public static Template getTemplateByFilename(String filename) throws IOException {
        return new Template(FileHandler.getFileContent(filename), getValuesFromConf(filename));
    }

    private static Map<String, String> getValuesFromConf(String name) throws IOException {
        return getRandomValues(parseValues(FileHandler.getFileContent(name.substring(0, name.lastIndexOf(".")) + ".conf")));
    }

    public static Template getTemplate(String filename, Map<String, String[]> conf) throws IOException {
        return new Template(FileHandler.getFileContent(filename), getRandomValues(conf));
    }

    public static Template getTemplate(String filename, String conf) throws IOException {
        return new Template(FileHandler.getFileContent(filename), getRandomValues(parseValues(conf)));
    }

    public static Map<String, String[]> parseValues(final String content, final String linebreak, final String token) {
        final Map<String, String[]> map = new HashMap<String, String[]>();
        final String[] row = content.split(linebreak == null ? LINEBREAK : linebreak);

        String[] column = null;
        for (int i = 0; i < row.length; i++) {
            column = row[i].split(token == null ? COLON : token);

            if (column.length != 3)
                throw new IllegalArgumentException("The configuration file is invalid.");
            else
                map.put(column[0], new String[] { column[1], column[2] });
        }

        return map;
    }

    public static Map<String, String[]> parseValues(final String content) {
        return parseValues(content, null, null);
    }

    public static Map<String, String> getRandomValues(Map<String, String[]> map) {
        final Map<String, String> values = new HashMap<String, String>();
        final Iterator<Entry<String, String[]>> iter = map.entrySet().iterator();

        Entry<String, String[]> entry = null;

        while (iter.hasNext()) {
            entry = iter.next();
            values.put(entry.getKey(), getTemplateValue(entry.getValue()[0], entry.getValue()[1]));
        }

        return values;
    }

    public static String getTemplateValue(String type, String specs) {
        if (INT.equals(type))
            return IdGenUtil.getIntId(Integer.parseInt(specs));
        else if (HEXA.equals(type))
            return IdGenUtil.getNewHexaId(Integer.parseInt(specs));
        else if (PATTERN.equals(type))
            return IdGenUtil.getIdWithPattern(specs);
        else if (DATE.equals(type))
            return new SimpleDateFormat(specs).format(new Date());
        else if (FIXED.equals(type))
            return specs;

        return BLANK;
    }
}